mod common;

use common::dourl;
use serde_json::json;

#[tokio::test]
async fn c_field() {
    let data = json!({
        "n": "ckgun,chunk,ckgun-t"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "ckgun",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "maxforce": 400,
                    "minf": 40
                }
            },
        ]
    });
    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "ckgun-t",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "k": {
                    "usn": "p 100 1",
                    "esn": "e001",
                },
                "v": {
                    "maxforce": 400,
                    "minf": 40
                }
            },
        ]
    });
    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "tab": "chunk",
        "line": "line-one",
        "data": [
            {
                "psn": "p1001",
                "wsn": "w001",
                "v": {
                    "d1":11,
                    "d2":21,
                    "d3":21,
                }
            },
        ]
    });
    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "ckgun,chunk,ckgun-t"
    });
    let ret = dourl(&data, "field").await.expect("error_10");
    assert_eq!(
        ret,
        json!({
            "chunk": ["d1", "d2", "d3", "dtp"],
            "ckgun": ["dtp", "maxforce", "minf"],
            "ckgun-t": ["dtp", "maxforce", "minf"]
        })
    );

    let data = json!({
        "n": "ckgun"
    });
    let ret = dourl(&data, "field").await.expect("error_10");
    assert_eq!(
        ret,
        json!({
            "ckgun": ["dtp", "maxforce", "minf"]
        })
    );

    let data = json!({
        "n": "ckgun-t"
    });
    let ret = dourl(&data, "field").await.expect("error_10");
    assert_eq!(
        ret,
        json!({
            "ckgun-t": ["dtp", "maxforce", "minf"]
        })
    );
}

#[tokio::test]
async fn c_tag() {
    let data = json!({
        "n": "ckgun,chunk,ckgun-t"
    });
    let ret = dourl(&data, "tag").await.expect("error_2");
    assert_eq!(
        ret,
        json!({
            "chunk": ["line", "psn", "ti", "wsn"],
            "ckgun": ["esn", "line", "psn", "ti", "usn", "wsn"],
            "ckgun-t": ["esn", "line", "psn", "ti", "usn", "wsn"]
        })
    );

    let data = json!({
        "n": "ckgun"
    });
    let ret = dourl(&data, "tag").await.expect("error_2");
    assert_eq!(
        ret,
        json!({
            "ckgun": ["esn", "line", "psn", "ti", "usn", "wsn"]
        })
    );

    let data = json!({
        "n": "ckgun-t"
    });
    let ret = dourl(&data, "tag").await.expect("error_2");
    assert_eq!(
        ret,
        json!({
            "ckgun-t": ["esn", "line", "psn", "ti", "usn", "wsn"]
        })
    );
}
